package com.tony.api;

import com.tony.utils.HttpUtil;
import com.tony.utils.Md5;
import com.tony.utils.TextUtil;
import net.sf.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * 获取验证码的方法
 */
public class ApiPhoneCode {


    public static final String host="http://api.ixinsms.com";//爱信
    public String username="ttoonnyy";
    public String password="abcd8888";
    public int projectId=21702;



    public ApiPhoneCode(String username,String password,int itemId){
        this.username=username;
        this.projectId=itemId;
        this.password=password;
    }

    /**
     * 登录速码平台
     */
    public String login(){
        String url=host+"/api/do.php?action=loginIn&name="+ username+"&password="+password;
        Map<String,String> map=new HashMap<>();
        map.put("Connection","Keep-Alive");
        String result= HttpUtil.getInstance(false,false).sendGet(url,map);
        if(!TextUtil.isEmpty(result)&&result.contains("|")){
            String string=result.substring(0,1);
            if("1".equals(string)){
                //System.out.println("登录手机获取平台成功");
                String token=result.substring(2,result.length());
                //System.out.println(token);
                return token;
            }else{
                System.out.println("登录手机获取平台失败："+result.substring(2,result.length()));
            }
        }
        return null;
    }


    /**
     * 获取手机号码
     */
    public String getPhone(String token){

        String mobile=null;
        while (true){
            String url=host+"/api/do.php?action=getPhone&sid="+projectId+"&token="+token;
            Map<String,String> map=new HashMap<>();
            map.put("Connection","Keep-Alive");
            String result= HttpUtil.getInstance(false,false).sendGet(url,map);
            System.out.println(result);

            if(!TextUtil.isEmpty(result)&&result.contains("|")){
                String string=result.substring(0,1);
                if("1".equals(string)){
                    System.out.println("获取手机号码成功");
                    mobile=result.substring(2,result.length());
                    System.out.println(mobile);
                    return mobile;
                }else{
                    System.out.println(result.substring(2,result.length()));
                    TextUtil.sleep(30);
                }
            }
        }
    }


    /**
     * 获取验证码
     */
    public String getCode(String phone,String token){

        String code=null;
        int i=1;
        while (TextUtil.isEmpty(code)){
            String url=host+"/api/do.php?action=getMessage&sid="+projectId+"&phone="+phone+"&token="+token;
            Map<String,String> map=new HashMap<>();
            map.put("Connection","Keep-Alive");
            String result= HttpUtil.getInstance(false,false).sendGet(url,map);
            //System.out.println(result);
            if(!TextUtil.isEmpty(result)&&result.contains("|")){
                String string=result.substring(0,1);
                if("1".equals(string)){
                    return TextUtil.getNumFromString(result.substring(2,result.length()));
                }else{
                    i++;
                    if(i>15){
                        return null;
                    }
                    System.out.println(result.substring(2,result.length()));
                    TextUtil.sleep(10);
                }
            }
        }
        System.out.println("登录手机获取平台失败");
        return null;

    }


    /**
     * 获取账户信息
     */
    public String getAccountInfo(String token){
        String url=host+"/api/do.php?action=getSummary&token="+token;
        Map<String,String> map=new HashMap<>();
        map.put("Connection","Keep-Alive");
        String result= HttpUtil.getInstance(false,false).sendGet(url,map);
        System.out.println(result);
        if(!TextUtil.isEmpty(result)&&result.contains("|")){
            String string=result.substring(0,1);
            if("1".equals(string)){
                System.out.println("获取账户信息成功");
                String temp=result.substring(2,result.length());
                System.out.println(temp);
                return temp;
            }else{
                System.out.println("获取账户信息成功"+result.substring(2,result.length()));
            }
        }
        return null;
    }


    /**
     * 获取电话号码
     */
    public String getPhone(){
        String sign= Md5.encrypt("11011xiaopy123456");
        String url="http://yunkong.xingxinghulian.com/api/Gateway.ashx?action=getPhoneNum&sid=11011&partnerName=xiaopy&sign="+sign;
        Map<String,String> headerMap=new HashMap<>();
        String result= HttpUtil.getInstance(false,false).sendGet(url,headerMap);
        System.out.println(result);

        if(!TextUtil.isEmpty(result)){
            JSONObject jsonObject= JSONObject.fromObject(result);
            if(jsonObject!=null&&jsonObject.has("data")){
                return jsonObject.getString("data");
            }
        }
        return null;
    }

    //获取验证码
    public String getCode(String phone){
        String sign= Md5.encrypt(phone+"11011xiaopy123456");
        String url="http://yunkong.xingxinghulian.com/api/Gateway.ashx?action=getSmsInfo&mobile="+phone+"&sid=11011&partnerName=xiaopy&sign="+sign;
        Map<String,String> headerMap=new HashMap<>();
        String result= HttpUtil.getInstance(false,false).sendGet(url,headerMap);
        System.out.println(result);

        if(!TextUtil.isEmpty(result)){
            JSONObject jsonObject= JSONObject.fromObject(result);
            if(jsonObject!=null&&jsonObject.has("data")){
                String data= jsonObject.getString("data");
                return TextUtil.getNumFromString(data);
            }
        }
        return null;
    }


    public static void main(String[] args){


    }


}
